<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-10-18
 * Time: 17:06
 */

namespace App\Model\Account;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Article extends Model
{
    /**
     * 文章列表
     */
    public static function articleList($where = [])
    {
        $list = DB::table('article')
            ->where($where)
            ->select('a_id', 'a_type', 'a_title', 'a_content', 'a_phone', 'a_status',
                DB::raw('from_unixtime(a_create_time) as a_create_time'),
                DB::raw('from_unixtime(a_update_time) as a_update_time'))
            ->get();
        if (sizeof($list) == 0) {
            return false;
        }
//        foreach($list as $k=>$v){
//            if (strlen($v->a_title) > 15) {
//                $v->a_title = mb_substr($v->a_title, 0, 15, 'utf-8').'...';
//            }
//        }
        return $list;
    }
    /**
     * 添加文章
     */
    public static function articleAdd($params)
    {
        $params['a_create_time'] = time();
        $params['a_update_time'] = time();
        if ($params['a_type'] == 1) {
            $aId = DB::table('article')->where('a_type', 1)->value('a_id');
            if ($aId) {
                return -1;
            }
        }
        return DB::table('article')->insert($params);
    }

    /**
     * 修改文章
     */
    public static function articleEdit($params)
    {
        $params['a_update_time'] = time();
        if ($params['a_type'] == 1) {
            $aId = DB::table('article')->where('a_type', 1)->where('a_id', '<>', $params['a_id'])->value('a_id');
            if ($aId) {
                return -1;
            }
        }
        return DB::table('article')->where('a_id', $params['a_id'])->update($params);
    }
    /**
     * 删除文章
     */
    public static function articleDel($aid)
    {
        return DB::table('article')->where('a_id', $aid)->delete();
    }
    /**
     * 禁用或者启用
     */
    public static function updateStatus($aid, $status)
    {
        if ($status == 1) {
            $params['a_status'] = 2;
        } else {
            $params['a_status'] = 1;
        }
        $params['a_update_time'] = time();
        return DB::table('article')->where('a_id', $aid)->update($params);
    }
//================================================   APP使用  ============================================
    /**获取公司简介  、使用帮助
     * @param $type
     * @return mixed
     */
    public static function getInfo($type)
    {
        return DB::table('article')
            ->where('a_type', $type)
            ->where('a_status', 1)
            ->orderBy('a_id', 'desc')
            ->select('a_title', 'a_content', 'a_phone')
            ->first();
    }
    /**
     * APP首页  新闻列表
     */
    public static function newsList($pageLimit)
    {
        $list = DB::table('article')
            ->where('a_type', 2)
            ->where('a_status', 1)
            ->orderBy('a_update_time', 'desc')
            ->select('a_id', 'a_title', 'a_content', DB::raw('from_unixtime(a_update_time) as a_update_time'))
            ->paginate($pageLimit);
        if (sizeof($list) == 0) {
            return false;
        }
        foreach($list as $k=>$v){
            if (strlen($v->a_title) > 15) {
                $v->a_title = mb_substr($v->a_title, 0, 15, 'utf-8').'...';
            }
        }
        return $list;
    }
}